<?php 
declare (strict_types = 1);
namespace app\admin\controller;
use app\common\controller\Admin;
use think\Request;
class ScriptRole extends Admin
{
    public function initialize()
    {
        $this->menu_input = [
            'all' => [
                ['type' => 'text', 'name' => '角色名称', 'key' => 'name','verify'=>'required'],
                ['type' => 'y_image', 'name' => '角色封面图', 'key' => 'image','verify'=>'required'],
                ['type' => 'textarea', 'name' => '角色介绍', 'key' => 'content','verify'=>'required'],
            ],
            'add' => [
            ],
            'upload' => [
            ],
        ];
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function table_mage(&$data = [])
    {
    
        $data = [
            ['field' => 'create_time', 'title' => '添加时间'],
            ['field' => 'update_time', 'title' => '修改时间'],
            ['title' => '操作', 'templet' => '#list_setting', 'width' => '7%']
        ];
        return parent::table_mage($data); // TODO: Change the autogenerated stub
    }
    //添加-数据接口
    public function add_ajax()
    {
        if (request()->isAjax()) {
            $post = array_filter(input('post.'));
            if ($post) {
                if(mb_strlen($post['name']) > 15) return msgerror('名字不能超过15个字符');
                if(mb_strlen($post['content']) > 500) return msgerror('介绍不能超过500个字符');
                $this->model->save($post);
            }
            return datasuccess($this->model->id);
        }
    }
    //编辑-数据接口
    public function upload_ajax()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            if ($post) {
                if(mb_strlen($post['name']) > 15) return msgerror('名字不能超过15个字符');
                if(mb_strlen($post['content']) > 500) return msgerror('介绍不能超过500个字符');
                $this->model::update($post);
            }
            return datasuccess($post);
        }
    }
}